package com.social.socialservice.controller;

import com.social.socialservice.dal.HotSearchDAL;
import com.social.socialservice.dal.PostDAL;
import com.social.socialservice.model.PostInfo;
import com.social.socialservice.unit.Pager;
import com.social.socialservice.unit.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户发布贴文相关请求API
 *
 */
@RestController
@CrossOrigin
public class PostController {
    @Autowired
    PostDAL postDAL;



    /**
     * 转发请求
     * 添加转发内容
     */
    @RequestMapping(value = "/addQuotePost", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response addQuotePost(@RequestBody PostInfo postInfo){
        postInfo.setZf_quick(0);
        if(postDAL.addPost(postInfo)){
            return new Response(200,"转发成功",null);
        }else{
            return new Response(203,"快转失败",null);
        }
    }

    /**
     * 加载帖子 测试功能
     */
    @RequestMapping(value = "/getPostData", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getPostData(){
        return postDAL.getPostData();
    }

    /**
     * 加载推荐推文分页数据封装
     */
    @RequestMapping(value = "/getForYouData", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getForYouData(@RequestBody Pager pager){
        return postDAL.getForYouPostData(pager);
    }

    /**
     * 加载指定用户贴文分页数据
     * getUserPostDataByUserId
     */
    @RequestMapping(value = "/getUserPostDataByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getUserPostDataByUserId(@RequestBody Pager pager){
        return postDAL.getUserPostDataByUserId(pager);
    }

    /**
     * 获取的是被转发的帖子基本信息用来引用的
     */
    @RequestMapping(value = "/getZFPostInfoByPostId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getZFPostInfoByPostId(@RequestBody PostInfo postInfo){
        System.out.println(postInfo.getPost_id());
        return postDAL.getPostInfoByPostId(postInfo);
    }

    /**
     * 根据post_id查询帖子信息
     * 包括帖子是否已被转发和点赞
     * @param 帖子id
     */
    @RequestMapping(value = "/getPostInfoByPostId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getPostInfoByPostId(@RequestBody PostInfo postInfo){
        System.out.println(postInfo.getPost_id());
        return postDAL.getPostInfoByPostId(postInfo);
    }

    /**
     * 根据post_id查询帖子信息
     * 包括帖子是否已被转发和点赞
     */
    @RequestMapping(value = "/getPostRelationByPostIdAndCurrentUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getPostRelationByPostIdAndCurrentUserId(@RequestBody PostInfo postInfo){
        return postDAL.getPostRelationShipByPostIdAndCurrentUserId(postInfo);
    }

    /**
     * 点赞操作
     */
    @RequestMapping(value = "/addPressXH", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response addPressXH(@RequestBody PostInfo postInfo){
        return postDAL.addPressXH(postInfo);
    }

    /**
     * 取消点赞操作
     */
    @RequestMapping(value = "/cancelPressXH", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response cancelPressXH(@RequestBody PostInfo postInfo){
        return postDAL.cancelPressXH(postInfo);
    }

    /**
     * 帖子访问量
     * 每访问一次+1
     */
    @RequestMapping(value = "/addFWCounts", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response addFWCounts(@RequestBody PostInfo postInfo){
        return postDAL.addFWCounts(postInfo);
    }


    /**
     * 快速转发：没有评论内容的帖子
     * 自动添加转发内容
     */
    @RequestMapping(value = "/addQuickZF", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response addQuickZF(@RequestBody PostInfo postInfo){
        //快速转发，设置zf_qucik字段为1
        postInfo.setZf_quick(1);
        if(postDAL.addQuotePost(postInfo)){
            return postDAL.addQuickZF(postInfo);
        }else{
            return new Response(203,"快转失败",null);
        }
    }

    @RequestMapping(value = "/cancelQuickZF", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response cancelQuickZF(@RequestBody PostInfo postInfo){
        return postDAL.cancelQuickZF(postInfo);
    }

    /**
     * getUserCommentDataMapper
     * 加载回复列表数据
     * 参数：user_id,currentPage,PageSize
     */
    @RequestMapping(value = "/getUserCommentDataMapper", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getUserCommentDataMapper(@RequestBody Pager pager){
        return postDAL.getUserCommentDataMapper(pager);
    }

    /**
     * getUserReplyPostDataMapperByUserId
     * 获取用户回复和转发数据分页
     * 参数：user_id,currentPage,PageSize
     */
    @RequestMapping(value = "/getUserReplyPostDataMapperByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getUserReplyPostDataMapperByUserId(@RequestBody Pager pager){
        return postDAL.getUserReplyPostDataMapperByUserId(pager);
    }

    /**     *
     * getUserLikePostDataMapperByUserId
     * 获取用户喜欢的分页数据
     * 参数：user_id,currentPage,PageSize
     */
    @RequestMapping(value = "/getUserLikePostDataMapperByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getUserLikePostDataMapperByUserId(@RequestBody Pager pager){
        return postDAL.getUserLikePostDataMapperByUserId(pager);
    }

    /**
     * getUserMediaPostDataMapperByUserId
     * 获取用户多媒体数据分页总数
     * 包括图片和视频
     * 参数：user_id,currentPage,PageSize
     */
    @RequestMapping(value = "/getUserMediaPostDataMapperByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getUserMediaPostDataMapperByUserId(@RequestBody Pager pager){
        return postDAL.getUserMediaPostDataMapperByUserId(pager);
    }

    /**
     * 嵌入热搜功能
     */
    @Autowired
    HotSearchDAL hotSearchDAL;

    /**
     * getPostSearchDataMapperByQuery
     * 查询带搜索条件的分页数据
     * 参数为搜索标题
     */
    @RequestMapping(value = "/getPostSearchDataMapperByQuery", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getPostSearchDataMapperByQuery(@RequestBody Pager pager){
        System.out.println(pager);
        return hotSearchDAL.getPostSearchDataMapperByQuery(pager);
    }

    /**
     * 获取热搜列表
     */
    @RequestMapping(value = "/getHotSearchList", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getHotSearchList(){
        return hotSearchDAL.getHotSearchList();
    }

    /**
     * getCurrentUserFoUserPostDataList
     * 获取当前用户关注的人的帖子列表
     * 1.先获取当前用户关注的人列表，并转换字符串
     * 2.用条件语句 in 来查询
     */
    @RequestMapping(value = "/getCurrentUserFoUserPostDataList", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response getPostMapperDataByCurrentUserFoUserIds(@RequestBody Pager pager){
        return postDAL.loadPostMapperDataByCurrentUserFoUserIds(pager);
    }

    /**
     * 删除自己的帖子
     * 这里是假删除，将帖子的字段post_delete设置为1
     */
    @RequestMapping(value = "/delPostByUserId", method = RequestMethod.POST, produces = "application/json; utf-8")
    public Response delPostByUserId(@RequestBody PostInfo postInfo){
        System.out.println("调用删除接口");
        return postDAL.delPostByUserId(postInfo);
    }
}
